//
//  RegisteredController.m
//  soccer
//
//  Created by 上官千城 on 15/9/10.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "ORegisteredController.h"
#import "OUserInfo.h"
#import "UIUtils.h"


@interface ORegisteredController ()

@end

@implementation ORegisteredController

-(void) layout
{
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //背景
    UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    backView.image = [UIImage imageNamed:@"注册第一步@2x"];
    [self.view addSubview:backView];
    //返回登陆界面
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setTitle:@"返回登录" forState:UIControlStateNormal];
    back.frame = CGRectMake(10, 40, 100, 20);
    [back addTarget:self action:@selector(backlogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back];
    //用户名
    self.userName = [[UITextField alloc]initWithFrame:CGRectMake(40, 150, WIDTH - 80, 30)];
    self.userName.borderStyle = UITextBorderStyleRoundedRect;
    self.userName.delegate = self;
    self.userName.placeholder = @"输入用户名";
    [self.view addSubview:self.userName];
    //密码
    self.password1 = [[UITextField alloc]initWithFrame:CGRectMake(self.userName.frame.origin.x, self.userName.frame.origin.y + self.userName.frame.size.height + 30, self.userName.frame.size.width, self.userName.frame.size.height)];
    self.password1.borderStyle = UITextBorderStyleRoundedRect;
    self.password1.secureTextEntry = YES;
    self.password1.delegate = self;
    self.password1.placeholder = @"输入密码";
    [self.view addSubview:self.password1];
    //确认密码
    self.password2 = [[UITextField alloc]initWithFrame:CGRectMake(self.password1.frame.origin.x, self.password1.frame.origin.y + self.password1.frame.size.height + 30, self.password1.frame.size.width, self.password1.frame.size.height)];
    self.password2.borderStyle = UITextBorderStyleRoundedRect;
    self.password2.placeholder = @"再次输入密码";
    self.password2.delegate = self;
    self.password2.secureTextEntry = YES;
    [self.view addSubview:self.password2];
    
    //注册并且登陆按钮
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    [login addTarget:self action:@selector(registeredAction) forControlEvents:UIControlEventTouchUpInside];
    login.frame = CGRectMake(30 , self.password2.frame.origin.y + self.password2.frame.size.height + 30, WIDTH / 3, 35);
    [login setImage:[UIImage imageNamed:@"完成注册1"] forState:UIControlStateNormal];
    [login setImage:[UIImage imageNamed:@"完成注册"] forState:UIControlStateSelected];
//    [login setTitle:@"注册登陆" forState:UIControlStateNormal];
//    login.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:login];
    //偏好设置按钮
    UIButton *Registered = [UIButton buttonWithType:UIButtonTypeCustom];
    Registered.frame = CGRectMake(WIDTH - login.frame.size.width - 30, login.frame.origin.y, login.frame.size.width, login.frame.size.height);
//    [Registered setTitle:@"偏好设置" forState:UIControlStateNormal];
//    Registered.backgroundColor = [UIColor orangeColor];
    [Registered setImage:[UIImage imageNamed:@"偏好设置1"] forState:UIControlStateNormal];
    [Registered setImage:[UIImage imageNamed:@"偏好设置"] forState:UIControlStateSelected];
    [Registered addTarget:self action:@selector(settingAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Registered];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self layout];
    
    self.regLogic = [[OUserRegLogic alloc] initWithCallBack:@selector(userRegCallBack:) for:self];
    self.loginLogic = [[OLoginLogic alloc] initWithCallBack:@selector(loginCallBack:) for:self];
    self.enterAppLogic = [[OEnterApp alloc] initWithCallBack:@selector(enterAppCallBack:) for:self];
}

-(void) userRegCallBack:(OBaseLogic*)logicObj
{
    NSLog(@"login start....");
    self.loginLogic.account = self.regLogic.account;
    self.loginLogic.pwd = self.regLogic.pwd;
    [self.loginLogic request];
}

-(void) loginCallBack:(OBaseLogic*)logicObj
{
    NSLog(@"%@",self.loginLogic.token);
    self.enterAppLogic.token = self.loginLogic.token;
    self.enterAppLogic.sign = self.loginLogic.sign;
    [self.enterAppLogic request];
    NSLog(@"enterApp start....");
}

-(void) enterAppCallBack:(OBaseLogic*)logicObj
{
    OUserInfo* info = [OUserInfo getInfo];
    if (!info) {
        info = [[OUserInfo alloc] init];
    }
    info.token = self.loginLogic.token;
    info.sign = self.loginLogic.sign;
    info.userId = self.enterAppLogic.userId;
    info.userName = self.loginLogic.account;
    
    [OUserInfo setInfo:info];
    //[info update];
    
    [self.myDelegate setRootViewControllerReg];
    
}

-(void)backlogin
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

-(void)registeredAction
{
    if (![self.password1.text isEqualToString:self.password2.text]) {
        [UIUtils showAlert:@"提示" message:@"两次密码不一致" left:@"确定" right:nil delegate:nil];
        return;
    }
    //注册用户名密码 赋值
    self.regLogic.account = self.userName.text;
    self.regLogic.pwd = self.password1.text;
    self.regLogic.phone = @"1234";
    self.regLogic.protuctId = @"123";
    
    [self.regLogic request];
}


//偏好设置
-(void)settingAction
{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.userName resignFirstResponder];
    [self.password1 resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.userName resignFirstResponder];
    [self.password2 resignFirstResponder];
    [self.password1 resignFirstResponder];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
